﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace Leaf.NetCore.Cache
{
    /// <summary>
    /// 版本：1.0
    /// 描述：Redis配置管理
    /// 日期：2018-11-5 17:49:09
    /// 创建人：王志海
    /// Copyright (c) 2015-2018 首溯科技有限公司
    /// </summary>
    public sealed class RedisConfigInfo
    {
        /// <summary>
        /// 可写的Redis链接地址 pwd@ip:port,pwd@ip:port
        /// </summary>
        public string WriteServerList { get; set; }
        /// <summary>
        /// 可读的Redis链接地址 pwd@ip:port,pwd@ip:port
        /// </summary>
        public string ReadServerList { get; set; }
        /// <summary>
        /// 最大写链接数 60
        /// </summary>
        public int MaxWritePoolSize { get; set; }
        /// <summary>
        /// 最大读链接数 60
        /// </summary>
        public int MaxReadPoolSize { get; set; }
        /// <summary>
        /// 自动重启
        /// </summary>
        public bool AutoStart { get; set; }
        /// <summary>
        /// 本地缓存到期时间，单位:秒 36000
        /// </summary>
        public int LocalCacheTime { get; set; }
        /// <summary>
        /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
        /// </summary>
        public bool RecordeLog { get; set; }

        /// <summary>
        /// <add key="redisconfig" value="WriteServerList=20180512sskj@39.106.210.230:6378;ReadServerList=20180512sskj@39.106.210.230:6378;MaxWritePoolSize=60;MaxReadPoolSize=60;AutoStart=true;LocalCacheTime=36000;RecordeLog=false" />
        /// </summary>
        /// <param name="sectionName"></param>
        public RedisConfigInfo(string redisconfig)
        {
            //string redisconfig = AppConfigurtaionServices.Configuration["Appsettings:redisconfig"].ToString();
            //string redisconfig = ConfigManagerHelper.Get<string>(sectionName).Trim(); //配置文件
            Dictionary<string, string> redisDict = new Dictionary<string, string>();
            string[] redisArray; //配置拆解
            string[] items; //过渡项
            redisArray = redisconfig.Split(';');
            foreach (var item in redisArray)
            {
                if (item == "") continue;
                items = item.Split('=');
                redisDict.Add(items[0], items[1]);
            }
            if (redisDict.ContainsKey("WriteServerList"))
            {
                this.WriteServerList = redisDict["WriteServerList"];
            }
            if (redisDict.ContainsKey("ReadServerList"))
            {
                this.ReadServerList = redisDict["ReadServerList"];
            }
            this.MaxWritePoolSize = 60;
            this.MaxReadPoolSize = 60;
            this.AutoStart = true;
            this.LocalCacheTime = 3600; //1小时
            this.RecordeLog = false;
            if (redisDict.ContainsKey("MaxWritePoolSize"))
            {
                this.MaxWritePoolSize = Convert.ToInt32(redisDict["MaxWritePoolSize"]);
            }

            if (redisDict.ContainsKey("MaxReadPoolSize"))
            {
                this.MaxReadPoolSize = Convert.ToInt32(redisDict["MaxReadPoolSize"]);
            }

            if (redisDict.ContainsKey("AutoStart"))
            {
                if (redisDict["AutoStart"].ToLower() == "false")
                {
                    this.AutoStart = false;
                }
            }
            if (redisDict.ContainsKey("LocalCacheTime"))
            {
                this.LocalCacheTime = Convert.ToInt32(redisDict["LocalCacheTime"]);
            }
            if (redisDict.ContainsKey("RecordeLog"))
            {
                if (redisDict["AutoStart"].ToLower() == "true")
                {
                    this.RecordeLog = true;
                }
            }

        }


        /// <summary>
        /// 可写的Redis链接地址
        /// </summary>
        //[ConfigurationProperty("WriteServerList", IsRequired = false)]
        //public string WriteServerList
        //{
        //    get
        //    {
        //        return (string)base["WriteServerList"];
        //    }
        //    set
        //    {
        //        base["WriteServerList"] = value;
        //    }
        //}


        /// <summary>
        /// 可读的Redis链接地址
        /// </summary>
        //[ConfigurationProperty("ReadServerList", IsRequired = false)]
        //public string ReadServerList
        //{
        //    get
        //    {
        //        return (string)base["ReadServerList"];
        //    }
        //    set
        //    {
        //        base["ReadServerList"] = value;
        //    }
        //}


        /// <summary>
        /// 最大写链接数
        /// </summary>
        //[ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
        //public int MaxWritePoolSize
        //{
        //    get
        //    {
        //        int _maxWritePoolSize = (int)base["MaxWritePoolSize"];
        //        return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5;
        //    }
        //    set
        //    {
        //        base["MaxWritePoolSize"] = value;
        //    }
        //}


        /// <summary>
        /// 最大读链接数
        /// </summary>
        //[ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
        //public int MaxReadPoolSize
        //{
        //    get
        //    {
        //        int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
        //        return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
        //    }
        //    set
        //    {
        //        base["MaxReadPoolSize"] = value;
        //    }
        //}


        /// <summary>
        /// 自动重启
        /// </summary>
        //[ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
        //public bool AutoStart
        //{
        //    get
        //    {
        //        return (bool)base["AutoStart"];
        //    }
        //    set
        //    {
        //        base["AutoStart"] = value;
        //    }
        //}




        /// <summary>
        /// 本地缓存到期时间，单位:秒
        /// </summary>
        //[ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)]
        //public int LocalCacheTime
        //{
        //    get
        //    {
        //        return (int)base["LocalCacheTime"];
        //    }
        //    set
        //    {
        //        base["LocalCacheTime"] = value;
        //    }
        //}


        /// <summary>
        /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
        /// </summary>
        //[ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
        //public bool RecordeLog
        //{
        //    get
        //    {
        //        return (bool)base["RecordeLog"];
        //    }
        //    set
        //    {
        //        base["RecordeLog"] = value;
        //    }
        //}

    }
}
